# -*- coding: utf-8 -*-
import scrapy


class Wu8carSpider(scrapy.Spider):
    name = 'wu8car'
    allowed_domains = ['58che.com','img2.xgo-img.com.cn']
    start_urls = ['https://www.58che.com/brand.html']

    # def parse(self, response):
    #     a_list = response.xpath('//div[@id="iconav"]/ul/li/a')
    #     for a in a_list:
    #         item={}
    #         item['level']=a.xpath('./text()').extract_first()
    #         item['level_url']='https:'+a.xpath('./@href').extract_first()
    #         print(item)
    #         yield scrapy.Request(
    #             item['level_url'],self.parse_list,meta={'item':item}
    #         )
    #         break
    #         pass
    #     pass

    def parse(self,response):
        # item=response.meta['item']
        a_list = response.xpath('//div[@class="r"]/ul/li/dl/dt/a')
        for a in a_list:
            item={}
            item['type']=a.xpath('./text()').extract_first()
            item['type_url'] = 'https:'+a.xpath('./@href').extract_first()
            print(item)
            yield scrapy.Request(
                item['type_url'],callback=self.parse_detail,meta={'item':item}
            )
            # break
            pass
        pass

    def parse_detail(self,response):
        print('----------------------')
        item = response.meta['item']
        item['pinpai'] = response.xpath('//div[@class="crumbs l"]/a[last()]/text()').extract_first()
        aa = response.xpath('//div[@id="line1"]')
        item['price']=aa.xpath('.//div[@class="canshu_q2"]//em/text()').extract_first()
        # 添加参数
        css=aa.xpath('.//ul[@class="canshu_list"]/li')
        titls = ['structure','displacement','gearbox','fuel_consumption','value_preservation_rate']
        values=[]
        for c in css[:-1]:
            values.append(c.xpath('./a/text()').extract_first())
        item.update(dict(zip(titls,values)))
        img_titles=['img_main','img_appearance', 'img_interior', 'img_space']
        # 添加图片
        images=aa.xpath('.//img')[:-1]
        img_hrefs = ['http:'+x.xpath('.//@src').extract_first() for x in images]
        item.update(dict(zip(img_titles, img_hrefs)))
        #车型报价
        car_type = response.xpath('//ul[@class="td"]')
        item['car_types']=[]
        for ct in car_type:
            c_type={}
            c_type['ml']=ct.xpath('./@ml').extract_first()
            c_type['year'] = ct.xpath('./@y').extract_first()
            c_type['pai_liang'] = ct.xpath('./@pl').extract_first()
            c_type['title'] = ct.xpath('.//p[@class="p1"]/a/text()').extract_first()
            c_type.update(dict(zip('qu_dong,bian_su_xiang,ji_xiang'.split(','),ct.xpath('.//p[@class="p2"]//text()').extract())))
            c_type['rd']=ct.xpath('./li[@class="t2"]/span/em/@style').extract_first()[6:-1]
            c_type['zhi_dao_jia']=ct.xpath('./li[@class="t4"]/span/text()').extract_first()
            c_type['min_price']=ct.xpath('./li[@class="t5"]/a/text()').extract_first()
            item['car_types'].append(c_type)
        # print(item)
        yield item